package com.hspedu;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;

public class ClassLoad_ {
    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入key");
        String key = sc.next();
        switch (key) {
            case "1":
                Dog dog = new Dog();
                dog.cry();
                break;
            case "2":
                //反射->动态加载
                //加载Person类
                Class cls = Class.forName("com.hspedu.Person");
                Object o = cls.newInstance();
                Method m = cls.getMethod("hi");
                m.invoke(o);
                System.out.println("ok");
                break;
            default:
                System.out.println("do nothing");
        }
    }
}

//因为new Dog()是静态加载,因此必须编写dog类
//Person类是动态加载,所以没有编写Person类也不会报错,只有当动态加载该类时,才会报错
class Dog{
    public void cry(){
        System.out.println("小狗汪汪叫");
    }
}
class Person{
    public void hi(){
        System.out.println("小孩 hi");
    }
}